<template>

	<view class="">
		<jxaddressmanage :addressData="addressData" :userChooseOrderAddress="userOrderAddress" @delete="deleteAddress" @edit="editAddress" @add="editAddress" @choose="choose">
		</jxaddressmanage>
	</view>
	<!-- <view class="">
		fesfe
	</view> -->
</template>

<script setup>
	import jxaddressmanage from "@/uni_modules/jx-address-manage/components/jx-address-manage/jx-address-manage.vue"
	import {
		userInfo
	} from "@/stores/userInfo.js"

	let UserInfo = userInfo()

	import {
		computed,
		ref
	} from "vue"
	import {
		onLoad,
		onShow,
		onUnload
	} from '@dcloudio/uni-app';


	let addressData = ref([])

	let userOrderAddress = ref({})//用户选中的地址

	let deleteAddress = (item) => {
		uni.showModal({
			title: '提示',
			content: '确定删除该地址吗？',
			success: async (res) => {
				if (res.confirm) {
					console.log( item.id,"地址id")
					
					let result = await UserInfo.deleteUserAddress({
						wxLoginCode:UserInfo.userinfoData.wxLoginCode,
						addressId:item.id
					})
					
					if(result){
						getUserAddress()
						uni.showToast({
							title: '删除成功',
							icon: 'success'
						});
					}else{
						uni.showToast({
							title: '删除失败',
							icon: 'none'
						});
					}
				}
			}
		})
	}

	let addNewAddress = () => {

		uni.navigateTo({
			url: '/pages/mine/addAddresshh'
		})


	}

	//获取用户的地址
	let getUserAddress = async () => {
		let res = await UserInfo.getUserAddressList({
			wxLoginCode: UserInfo.userinfoData.wxLoginCode
		})

		addressData.value = res
		// console.log(res, "则是地址吗")
	}

	//编辑地址
	let editAddress = (e) => {
		// console.log(e, "编辑地址")
		uni.navigateTo({
			url: '/pages/mine/addAddresshh',
			success: function(res) {
				// 通过eventChannel向被打开页面传送数据
				res.eventChannel.emit('acceptDataFromOpenerPage', {
					data: e
				})
			}
		})
	}
	
	//选中地址
	let choose = (e)=>{
			// console.log(e,"选中地址")
			userOrderAddress.value = e
			uni.setStorageSync('userOrderAddress', e)
	}
	
	
	// 获取用户选中的地址
	let getUserChooseAddressData = ()=>{
		let results = uni.getStorageSync('userOrderAddress') || {}
		userOrderAddress.value = results
	}
	

	onShow(() => {
		getUserAddress()
		getUserChooseAddressData()
		
	})
</script>

<style>
</style>